﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient;

namespace Code.Manufacture
{
    public class ManufactureDataAccess
    {
        protected DataProvider m_DataProvider = DataProvider.GlobalDataProvider;
        public List<string> GetListManufactureName()
        {
            List<string> lst = new List<string>();

            string sSql = "SELECT TENNSX FROM NHA_SAN_XUAT";
            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSql);
            if (dt != null)
            {
                int iCount = dt.Rows.Count;
                for (int i = 0; i < iCount; i++)
                {
                    string sName = dt.Rows[i][0].ToString();
                    lst.Add(sName);
                }
            }
            return lst;
        }

        public List<ManufactureInfo> GetListManufacture()
        {
            List<ManufactureInfo> lst = new List<ManufactureInfo>();
            string sSql = "SELECT * FROM NHA_SAN_XUAT";
            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSql);

            if (dt != null)
            {
                int iCount = dt.Rows.Count;
                for (int i = 0; i < iCount; i++)
                {
                    int iID = int.Parse(dt.Rows[i][0].ToString());
                    string sName = dt.Rows[i][1].ToString();
                    string sNation = dt.Rows[i][2].ToString();
                    ManufactureInfo mInfo = new ManufactureInfo();
                    mInfo.ID = iID;
                    mInfo.Name = sName;
                    mInfo.Nation = sNation;
                    lst.Add(mInfo);
                }
            }
            return lst;
        }

        public ManufactureInfo GetManufactureByID(int iID)
        {
            ManufactureInfo mInfo = null;
            string sSQL = "Select * from NHA_SAN_XUAT where ID = '" + iID.ToString() + "'";
            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSQL);
            if(dt != null)
            {
                mInfo = new ManufactureInfo();
                string sName = dt.Rows[0][1].ToString();
                string sNation = dt.Rows[0][2].ToString();
                mInfo.ID = iID;
                mInfo.Name = sName;
                mInfo.Nation = sNation;
            }
            return mInfo;
        }

        public string GetManufactureNameByID(int iID)
        {
            string sName = string.Empty;
            string sSQL = "Select * from NHA_SAN_XUAT where ID = '" + iID.ToString() + "'";
            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSQL);
            if (dt != null)
            {               
                sName = dt.Rows[0][1].ToString();
            }
            return sName;
        }
        public List<string> GetListManufactureNameByCatelogyID(int iCatelogyID)
        {
            List<string> lst = new List<string>();

            string sSql = "Select distinct TenNSX from NHA_SAN_XUAT, SAN_PHAM WHERE NHA_SAN_XUAT.id = SAN_PHAM.idNSX AND SAN_PHAM.IDLOAISANPHAM = '" + iCatelogyID.ToString() + "'";
            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSql);
            if(dt != null)
            {
                int iCount = dt.Rows.Count;
                for(int i =0;i<iCount ; i++)
                {
                    string sName = dt.Rows[i][0].ToString();
                    lst.Add(sName);
                }
            }
            return lst;
        }

        public int GetManufactureIdByName(string sManufactureName)
        {
            int iID = -1;
            string sSql = "Select ID from NHA_SAN_XUAT where TenNSX = " + sManufactureName;
            DataTable dt = m_DataProvider.ExecuteQueryDataTable(sSql);
            if(dt!= null)
            {

                 iID = int.Parse(dt.Rows[0][0].ToString());
            }

            return iID;
        }
    }
}
